package com.pucheng.bracelet.app.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Method;

public class Configure {
    public static int screenHeight = 0;
    public static int screenWidth = 0;
    public static float screenDensity = 0;
    public static int densityDpi = 0;
    public static float xdpi = 0;
    public static float ydpi = 0;

    public static void init(Activity context) {
        if (screenDensity == 0 || screenWidth == 0 || screenHeight == 0) {
            DisplayMetrics dm = new DisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                context.getWindowManager().getDefaultDisplay().getRealMetrics(dm);
            } else {
                context.getWindowManager().getDefaultDisplay().getMetrics(dm);
                int height = getHeight(context);
                if (height > 0) {
                    dm.heightPixels = height;
                }
            }
            Configure.screenDensity = dm.density;
            Configure.screenHeight = dm.heightPixels;
            Configure.screenWidth = dm.widthPixels;
            Configure.densityDpi = dm.densityDpi;
            Configure.xdpi = dm.xdpi;
            Configure.ydpi = dm.ydpi;

        }
    }

    /**
     * 不含状态栏
     * @param context
     * @return
     */
    public static int getHeight(Activity context) {
        int height = 0;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        @SuppressWarnings("rawtypes")
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, displayMetrics);
            height = displayMetrics.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return height;
    }

    /**
     * 含状态栏
     * @param context
     * @return
     */
    public static int getHeightTitle(Activity context) {
        int heightTitle = 0;
        /**
         * 获取状态栏高度——方法4
         * 状态栏高度 = 屏幕高度 - 应用区高度
         * *注意*该方法不能在初始化的时候用
         * */
        //屏幕
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        //应用区域
        Rect outRect1 = new Rect();
        context.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
         heightTitle = dm.heightPixels - outRect1.height();  //状态栏高度=屏幕高度-应用区域高度
        return heightTitle;
    }

    public static void setHideVirtualKeyDelay(final Window window) {
        if (window == null) return;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setHideVirtualKey(window);
            }
        }, 100);
    }

    /*
    * 		// 隐藏标题栏
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            // 隐藏状态栏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    */
    public static void setHideVirtualKey(Window window) {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = window.getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //保持布局状态
//            int uiOptions =
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
//                            //布局位于状态栏下方
//                            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
//                            //全屏
//                            View.SYSTEM_UI_FLAG_FULLSCREEN|
//                            //隐藏导航栏
//                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
//                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            /**
             * 只隐藏底部导航
             */
//            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;


            int uiOptions = window.getDecorView().getSystemUiVisibility();
            int flags =
//                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    |
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
//                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            uiOptions |= flags;

            /**
             * 同时隐藏标题栏和底部导航栏
             */
/*            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                   | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;//标题栏*/

            window.getDecorView().setSystemUiVisibility(uiOptions);
        }
    }
    public static void setStickFullScreen(View view) {
        int systemUiVisibility = view.getSystemUiVisibility();
        int flags = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        systemUiVisibility |= flags;
        view.setSystemUiVisibility(systemUiVisibility);
    }
}
